package com.zbum.example.socket.server;

import org.junit.Test;

public class Test1 {
    /**
     * 14millis
     */
    @Test
    public void test1() {
        Long start=System.currentTimeMillis();
        byte[] bytes = new byte[2];
        bytes[0] = 10;
        bytes[1] = 98;
        for (int i = 0; i < 1000000; i++) {
            Integer u = (bytes[0] >= 0 ? bytes[0] : bytes[0] + 256) * 256 + (bytes[1] >= 0 ? bytes[1] : bytes[0] + 256);
        }
        Long end=System.currentTimeMillis();
        Long d=end-start;
        System.out.println("complete in :"+d+"millis");
    }

    /**
     * 260millis
     */
    @Test
    public void test2() {
        Long start=System.currentTimeMillis();
        byte[] bytes = new byte[2];
        bytes[0] = 10;
        bytes[1] = 98;
        for (int i = 0; i < 1000000; i++) {
            String a=this.BinaryToHexString(bytes);
            Integer b=Integer.parseInt(a,16);

        }
        Long end=System.currentTimeMillis();
        Long d=end-start;
        System.out.println("complete in :"+d+"millis");

    }

    private static String hexStr = "0123456789ABCDEF";  //全局

    public static String BinaryToHexString(byte[] bytes) {

        String result = "";
        String hex = "";
        for (int i = 0; i < bytes.length; i++) {
            //字节高4位
            hex = String.valueOf(hexStr.charAt((bytes[i] & 0xF0) >> 4));
            //字节低4位
            hex += String.valueOf(hexStr.charAt(bytes[i] & 0x0F));
            result += hex;
        }
        return result;
    }
}


